//
//  SZLGuideViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/19.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLGuideViewController.h"
#import "GuideCollectionCell.h"
#import "SZLTabBarViewController.h"
#import "MPageControl.h"

@interface SZLGuideViewController()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) NSArray *imageArr;
@property (nonatomic,weak) MPageControl *pageControl;

@end

@implementation SZLGuideViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.imageArr = @[@"guide01.png",@"guide02.png",@"guide03.png",@"guide04.png",@"guide05.png"];
    [self initCollectionView];
}
- (void)initCollectionView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(ScreenW, ScreenH);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flow];
    [collection registerClass:[GuideCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([GuideCollectionCell class])];
    collection.dataSource = self;
    collection.delegate = self;
    collection.backgroundColor = [UIColor whiteColor];
    collection.pagingEnabled = YES;
    collection.showsHorizontalScrollIndicator = NO;
    collection.bounces = NO;
    [self.view addSubview:collection];
    
    CGRect rect = self.view.bounds;
    rect.origin.y = rect.size.height - 35;
    rect.size.height = 30;
    MPageControl *pageControl = [[MPageControl alloc] initWithFrame:rect];
    pageControl.backgroundColor = [UIColor clearColor];
    pageControl.pointSize = CGSizeMake(10, 10);
    pageControl.numberOfPages = self.imageArr.count;
    pageControl.currentPage = 0;
    self.pageControl = pageControl;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = RGBColor(60, 168, 248);
    [_pageControl setImagePageStateNormal:GetImage(@"big.png")];
    [_pageControl setImagePageStateHighlighted:GetImage(@"small.png")];
    //[self.view addSubview:_pageControl];
    [_pageControl updateDots];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GuideCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([GuideCollectionCell class]) forIndexPath:indexPath];
    cell.imageName = self.imageArr[indexPath.row];
    cell.isHiden = YES;
    cell.delegate = self;
    if (indexPath.row == self.imageArr.count-1) {
        cell.inView.hidden = NO;
    }else {
        cell.inView.hidden = YES;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.pageControl.currentPage = indexPath.item;
    [self.pageControl updateDots];
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *path = collectionView.indexPathsForVisibleItems.lastObject;
    if (path.item == self.imageArr.count-1) {
        GuideCollectionCell *curCell = (GuideCollectionCell *)[collectionView cellForItemAtIndexPath:path];
        [curCell startAnimation];
    }
}

@end
